package com.mogu_soup.util;

import com.mogu_soup.entity.Bomb;

import java.util.Vector;

/**
 * Version: 2.1
 * Author: TangJun
 * Date: 2024-08-03
 * Note:爆炸对象池
 */
public class BombPool {
    private static final int DEFAULT_SIZE = 200;//默认爆炸池大小
    private static final int MAX_SIZE = 500;//
    private static Vector<Bomb> pool;//爆炸池
    //用静态代码块初始化，在类加载时就创建爆炸对象
    static {
        pool = new Vector<Bomb>(DEFAULT_SIZE);
        for (int i = 0; i < DEFAULT_SIZE; i++) {
            pool.add(new Bomb(0,0,0));
        }
    }

    //获取爆炸池中对象
    public static synchronized Bomb removeBomb() {
        return pool.isEmpty() ? new Bomb(0,0,0) : pool.remove(0);
    }

    //归还爆炸池中对象
    public static synchronized void addBomb(Bomb bomb) {
        if (pool.size() < MAX_SIZE) {
            pool.add(bomb);
        }
    }
}
